<?php

define('IN_NETGORESP', true);

 /*
   ----------------------------------------------------------------------------------------------------
   INCLUSIONS
   ----------------------------------------------------------------------------------------------------
	+ Common.php
		- Includes all the other files needed.
   ----------------------------------------------------------------------------------------------------
   */
 include("common.php");
 
 /*
   ----------------------------------------------------------------------------------------------------
   LOGIN SCRIPT
   ----------------------------------------------------------------------------------------------------
	+ Check to see if user wanted to logout
		- if so log them out.
	+ Check for login submission
		+ If there was one:
			+ See if the user exists
				- If so log them in and send them to their profile!
			+ Else
				- Give them huge error!
   ----------------------------------------------------------------------------------------------------
   */
 if(isset($_GET['logout']))
 {
 	if($userinfo == false)
 		header("Location: ".$link['login']);
 	else
 	{
 		setcookie("netgoresp", "", time()-100000, "/", "", 0);
 		header("Location: ".$link['login']);
 	}
 	die();
 }
 else
 {
	// Check for a submission
  if (isset($_POST["submit"]))
  {

		// Parse the MySQL Query
		$query = checkLogin($db, $_POST["username"], $_POST["password"]);

		// Check Database Rows
		if ($db->sql_rows($query) != 1) 
			die($template->error("Login Error", "Invalid username or password. Please go back and try again.")); 
		
		// Get an Array from the MySQL Query
		$row = $db->sql_array($query);
		if (isset($_POST["rememberme"])) 
		{ 
			$expiretime = time()+31536000; 
			$rememberme = 1; 
		} 
		else 
		{ 
			$expiretime = 0; 
			$rememberme = 0; 
		}
				
		$cookie = $row["name"] . " " . md5($row["password"]) . " " . $rememberme;
		setcookie("netgoresp", $cookie, $expiretime, "/", "", 0);
		header("Location: ".$link['main']);
	  die();
  }
	else 
	{
		// @template: Header
		include("template/".$config['template_name']."/header.php");
		
		// @template: Login Form
		include("template/".$config['template_name']."/login.form.php");
		
		// @template: Footer
		include("template/".$config['template_name']."/footer.php");
	}
 }

?>